﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace CollisionDetectionSandbox
{
    class ScoreUpdater
    {
        List<int> score_list;
        List<string> name_list;

        string FOLDERPATH = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) +
                "\\TeamStudios\\OlympianDefense\\";
        string FILEPATH
        {
            get { return FOLDERPATH + "Scores.txt"; }
        }

        public ScoreUpdater()
        {
            score_list = new List<int>();
            name_list = new List<string>();

            if (!Directory.Exists(FOLDERPATH))
            {
                Directory.CreateDirectory(FOLDERPATH);
            }

            if (!File.Exists(FILEPATH))
            {
                Stream fs = File.Create(FILEPATH);
                fs.Close();

                File.WriteAllText(FILEPATH, "0\n \n0\n \n0\n \n0\n \n0\n \n0\n \n0\n \n0\n \n0\n \n0\n \n");
            }
        }
        public static bool newHighScore = false;

        public void addNewScore(int sc, string name)
        {
            StreamReader readScores = File.OpenText(FILEPATH);
            string input = "";
            int count = 0;
            while ((input = readScores.ReadLine()) != null)
            {
                if (count % 2 == 0)
                {
                    score_list.Add(Int32.Parse(input));
                }
                else if (count % 2 == 1)
                {
                    name_list.Add(input);
                }
                count++;
            }
            readScores.Close();
            for (int x = 0; x < score_list.Count; x++)
            {
                if (sc > score_list[x])
                {
                    score_list.Insert(x, sc);
                    name_list.Insert(x, name);
                    x = score_list.Count;
                    newHighScore = true;
                }
                
            }
            StreamWriter writeScore = File.CreateText(FILEPATH);

            for (int x = 0; x < score_list.Count - 1; x++)
            {
                writeScore.WriteLine(score_list[x]);
                writeScore.WriteLine(name_list[x]);
            }
            writeScore.Close();
        }

        public List<int> getScores()
        {
            List<int> Scores = new List<int>();
            StreamReader readScores = File.OpenText(FILEPATH);
            string input = "";
            int count = 0;
            while ((input = readScores.ReadLine()) != null)
            {
                if (count % 2 == 0)
                {
                    Scores.Add(Int32.Parse(input));
                }
                else if (count % 2 == 1)
                {
                }
                count++;
            }
            readScores.Close();

            return Scores;
        }

        public List<string> getNames()
        {
            List<string> Names = new List<string>();
            StreamReader readNames = File.OpenText(FILEPATH);
            string input = "";
            int count = 0;
            while ((input = readNames.ReadLine()) != null)
            {
                if (count % 2 == 0)
                {
                }
                else if (count % 2 == 1)
                {
                    Names.Add(input);
                }
                count++;
            }
            readNames.Close();
            return Names;
        }
    }
}
